using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public static GameManager instance; // 单例模式的实例
    public Player player; // 玩家对象
    public PoolManager pool; // 对象池

    public float gameTime; // 游戏时间
    public float maxGameTime = 5 * 10f; // 最大游戏时间

    public float health;//当前生命值
    public int maxHealth = 100;//最大生命值
    public int level;//等级
    public int kill;//杀敌数
    public int exp;//经验值

    public LevelUp uiLevelUp;
    public GameObject uiResult;
    public GameObject uiVictroy;
    public GameObject enemyCleaner;
    public Joystick joystick;//虚拟摇杆
    public bool isLive;
    public int playerId;
    public int[] nextExp = { 3, 5, 10, 100, 150, 210, 280, 360, 450, 600 };//升级梯度
    void Awake()
    {
        instance = this;
        Application.targetFrameRate = 60;
    }

    public void GameStart(int id)
    {
        playerId = id;
        health = maxHealth;
        player.gameObject.SetActive(true);
        uiLevelUp.Select(playerId % 2);
        Resume();

        AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
        AudioManager.instance.PlayBgm(true);
    }

    void Update()
    {
        if (!GameManager.instance.isLive) return;
        gameTime += Time.deltaTime; // 更新游戏时间

        // 将游戏时间限制在最大游戏时间内
        if (gameTime >= maxGameTime)
        {
            gameTime = maxGameTime;
            GameVictroy();
        }
    }

    //加经验和升级
    public void GetExp()
    {
        //胜利了不需要经验
        if (!GameManager.instance.isLive) return;
        exp++;
        if (exp == nextExp[Mathf.Min(level, nextExp.Length - 1)])
        {
            level++;

            exp = 0;
            uiLevelUp.Show();
        }
    }

    public void Stop()
    {   
        joystick.gameObject.SetActive(false);
        isLive = false;
        Time.timeScale = 0;
    }
    public void Resume()
    {
        joystick.gameObject.SetActive(true);
        isLive = true;
        Time.timeScale = 1;
    }

    public void GameOver()
    {
        StartCoroutine(GameOverRoutine());
    }

    IEnumerator GameOverRoutine()
    {
        // 设置玩家为死亡状态
        isLive = false;

        // 等待0.5秒
        yield return new WaitForSeconds(0.5f);
        uiResult.SetActive(true);
        // 停止游戏逻辑或重置游戏状态
        Stop();

        AudioManager.instance.PlayBgm(false);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Lose);
    }

    public void GameRetry()
    {
        // 重新加载场景（假设索引为0的场景是起始场景）
        SceneManager.LoadScene(0);
    }

    public void GameVictroy()
    {
        StartCoroutine(GamevictroyRoutine());
    }

    IEnumerator GamevictroyRoutine()
    {
        // 设置玩家为死亡状态
        isLive = false;

        // 激活敌人清除器游戏对象
        enemyCleaner.SetActive(true);

        // 等待0.5秒
        yield return new WaitForSeconds(0.5f);

        // 激活 UI 结果游戏对象
        uiVictroy.SetActive(true);

        // 停止游戏逻辑或重置游戏状态
        Stop();

        AudioManager.instance.PlayBgm(false);
        AudioManager.instance.PlaySfx(AudioManager.Sfx.Win);
    }

    public void GameQuit()
    {
        Application.Quit();
    }
}
